package server.webEE.bean;

import java.math.BigInteger;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import weblogic.logging.NonCatalogLogger;

public class TestEntityBean implements EntityBean {

	private static final NonCatalogLogger NON_CATALOG_LOGGER = new NonCatalogLogger(
			TestEntityBean.class.getName());
	// public static final org.apache.logging.log4j.Logger NON_CATALOG_LOGGER =
	// LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
	/**
	 * 
	 */
	private static final long serialVersionUID = 6895160942622926194L;

	private EntityContext context;
	private BigInteger id;
	private String echo;

	public BigInteger fact() {
		BigInteger n = BigInteger.valueOf(1);
		for (; -1 >= n.compareTo(this.getId());)
			n = n.multiply(n.add(BigInteger.ONE));
		NON_CATALOG_LOGGER.error(n.toString());
		return n;
	}

	public String getEcho() {
		NON_CATALOG_LOGGER.error("getEcho_" + echo);
		return echo;
	}

	private Connection getConJDBC() throws SQLException {
		Context context;
		DataSource dataSource = null;
		try {
			context = new InitialContext();

			dataSource = (DataSource) context.lookup("java:comp/env/jdbc/orcl");
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dataSource.getConnection();
	}

	public Collection ejbFindByEcho(String echo) {
		NON_CATALOG_LOGGER.error("ejbFindByEcho");
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		Collection c = new ArrayList();
		try {
			connection = this.getConJDBC();
			preparedStatement = connection
					.prepareStatement("select ID from Test");

			if (1 != preparedStatement.executeUpdate())
				throw new EJBException(echo);
			c.add(new BigInteger("0"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (preparedStatement != null)
				try {
					preparedStatement.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			if (connection != null)
				try {
					connection.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		return c;
	}

	public void setEcho(String echo) {
		this.echo = echo;
		NON_CATALOG_LOGGER.error("setEcho_" + this.echo);
	}

	public BigInteger getId() {
		return id;
	}

	public EntityContext getContext() {
		return context;
	}

	public void stageLog(String string) {
		NON_CATALOG_LOGGER.error(string);
	}

	public BigInteger ejbFindByPrimaryKey(BigInteger key)
			throws FinderException {
		NON_CATALOG_LOGGER.error("ejbFindByPrimaryKey_" + key);

		return key;

	}

	public BigInteger ejbCreate(Integer id, String string) throws EJBException {
		this.id = BigInteger.valueOf(id);
		this.echo = string;
		NON_CATALOG_LOGGER.error("ejbCreate_" + this.id + "_" + this.echo);
		return this.id;
	}

	public void ejbPostCreate(Integer id, String string) {
		NON_CATALOG_LOGGER.error("ejbPostCreate");
	}

	public void ejbActivate() throws EJBException, RemoteException {
		NON_CATALOG_LOGGER.error("ejbActivate");
	}

	public void ejbLoad() throws EJBException, RemoteException {
		NON_CATALOG_LOGGER.error("ejbLoad");

	}

	public void ejbPassivate() throws EJBException, RemoteException {
		NON_CATALOG_LOGGER.error("ejbPassivate");
	}

	public void ejbRemove() throws RemoveException, EJBException,
			RemoteException {
		NON_CATALOG_LOGGER.error("ejbRemove");

	}

	public void ejbStore() throws EJBException, RemoteException {
		NON_CATALOG_LOGGER.error("ejbStore");

	}

	public void setEntityContext(EntityContext arg0) throws EJBException,
			RemoteException {
		this.context = arg0;
		NON_CATALOG_LOGGER.error("setEntityContext");
	}

	public void unsetEntityContext() throws EJBException, RemoteException {
		NON_CATALOG_LOGGER.error("unsetEntityContext");
		this.context = null;

	}
}
